package dao_base

import (
	"wrblog-api-go/app/model"
	"wrblog-api-go/app/model/model_base"
	"wrblog-api-go/pkg/errs"
	"wrblog-api-go/pkg/mysql"
	"wrblog-api-go/pkg/utils"
)

func GetArticleLibraryPageList(baseArticleLibrary model_base.SelectBaseArticleLibrary) (baseArticleLibrarys []model_base.BaseArticleLibraryVo, total int64, err error) {
	db := mysql.Db("salve-1").Model(&model_base.BaseArticleLibraryPo{})
	if baseArticleLibrary.UserId != 0 {
		db = db.Where("user_id = ?", baseArticleLibrary.UserId)
	}
	if baseArticleLibrary.LibraryName != "" {
		db = db.Where("library_name like ?", "%"+baseArticleLibrary.LibraryName+"%")
	}
	if baseArticleLibrary.LibraryUrl != "" {
		db = db.Where("library_url like ?", "%"+baseArticleLibrary.LibraryUrl+"%")
	}
	if baseArticleLibrary.IsOpen != "" {
		db = db.Where("is_open = ?", baseArticleLibrary.IsOpen)
	}
	if baseArticleLibrary.Status != "" {
		db = db.Where("status = ?", baseArticleLibrary.Status)
	}
	if !baseArticleLibrary.StartTime.IsZero() {
		db = db.Where("create_time >= ?", baseArticleLibrary.StartTime)
	}
	if !baseArticleLibrary.EndTime.IsZero() {
		db = db.Where("create_time <= ?", baseArticleLibrary.EndTime)
	}
	db.Where("deleted = '0'").Count(&total).Order("create_time desc")
	if baseArticleLibrary.PageNum != nil && baseArticleLibrary.PageSize != nil {
		db = db.Limit(*baseArticleLibrary.PageSize).Offset((*baseArticleLibrary.PageNum - 1) * *baseArticleLibrary.PageSize)
	} else {
		db = db.Limit(10)
	}
	err = db.Find(&baseArticleLibrarys).Error
	err = errs.ErrNotFound(err)
	return
}

func GetArticleLibraryInfo(libraryId int) (baseArticleLibrary model_base.BaseArticleLibraryVo, err error) {
	db := mysql.Db("salve-1").Model(&model_base.BaseArticleLibraryPo{})
	err = db.Where("library_id = ? and deleted = '0'", libraryId).First(&baseArticleLibrary).Error
	err = errs.ErrNotFound(err)
	return
}

func AddArticleLibrary(baseArticleLibrary model_base.BaseArticleLibraryPo) (int, error) {
	db := mysql.Db("salve-1")
	res := db.Create(&baseArticleLibrary)
	return baseArticleLibrary.LibraryId, errs.ErrNotFound(res.Error)
}

func EditArticleLibrary(baseArticleLibrary model_base.BaseArticleLibraryPo) (int64, error) {
	db := mysql.Db("salve-1")
	res := db.Updates(&baseArticleLibrary)
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

// RemoveArticleLibrary 逻辑删除
func RemoveArticleLibrary(libraryIds []string, uk string) (int64, error) {
	db := mysql.Db("salve-1").Model(&model_base.BaseArticleLibraryPo{})
	db.Where("library_id in (?)", libraryIds)
	res := db.Updates(&model_base.BaseArticleLibraryPo{BaseEntity: model.BaseEntity{UpdateTime: utils.NewTimeNow(), UpdateBy: uk, Deleted: "1"}})
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

// DeletedArticleLibrary 删除
func DeletedArticleLibrary(libraryIds []string) (int64, error) {
	db := mysql.Db("salve-1").Model(&model_base.BaseArticleLibraryPo{})
	db.Where("library_id in (?)", libraryIds)
	res := db.Delete(&model_base.BaseArticleLibraryPo{})
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}
